package schedule;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.List;
import model.TContract;
import model.contract.Loaninterest;
import dao.LoaninterestDao;
import dao.TContractDao;
import org.springframework.scheduling.annotation.Scheduled;

import util.Time;

public class CompoundJob {
	private TContractDao contractDao;
	private LoaninterestDao loaninterestDao;
	
	@Scheduled(cron="0 0 0 * * ?") //每天晚上12点
	public void abc(){
		List<Loaninterest> list=loaninterestDao.findModel("lastdate < curdate()");
		Timestamp nowdate=Time.CurrentTimestamp();
		System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~"+nowdate);
		for(Loaninterest li:list){
			TContract ctc=contractDao.findOne("id",li.getContractid());
			BigDecimal fafangjie=new BigDecimal(li.getGrantmoney());
			BigDecimal lilv=new BigDecimal(ctc.getPerformrates()).setScale(5, BigDecimal.ROUND_HALF_UP);
			BigDecimal lixi=sumInterest(fafangjie,lilv,li.getLastdate(),nowdate);
			li.setCompound(lixi.doubleValue());
			loaninterestDao.update(li);
		}
	}

	//计算利息
	private BigDecimal sumInterest(BigDecimal benjin,BigDecimal lilv,Timestamp begindate,Timestamp enddate){
		//日期长度
		int twoDaysLenght=Time.datediff(begindate,enddate);
		//积数
		BigDecimal jishu=benjin.multiply(new BigDecimal(Double.valueOf(twoDaysLenght))).setScale(2, BigDecimal.ROUND_HALF_UP);
		//360*100
		BigDecimal m360=new BigDecimal(36000);
		return jishu.multiply(lilv).divide(m360,5,BigDecimal.ROUND_HALF_UP).setScale(2, BigDecimal.ROUND_HALF_UP);
	}
	
	public TContractDao getContractDao() {
		return contractDao;
	}

	public void setContractDao(TContractDao contractDao) {
		this.contractDao = contractDao;
	}

	public LoaninterestDao getLoaninterestDao() {
		return loaninterestDao;
	}

	public void setLoaninterestDao(LoaninterestDao loaninterestDao) {
		this.loaninterestDao = loaninterestDao;
	}
}
